package com.neuedu.hisboot.controller;

import com.neuedu.hisboot.dto.Result;
import com.neuedu.hisboot.entity.Medicalrecord;
import com.neuedu.hisboot.service.IMedicalrecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  电子病历管理控制器
 * </p>
 *
 * @author jaun
 * @since 2025-07-04
 */
@RestController
@RequestMapping("/medicalrecord")
public class MedicalrecordController {

    @Autowired
    private IMedicalrecordService medicalrecordService;

    /**
     * 获取所有病历记录
     */
    @GetMapping("/list")
    public Result<List<Medicalrecord>> getMedicalRecords() {
        List<Medicalrecord> records = medicalrecordService.list();
        return Result.success(records);
    }

    /**
     * 根据ID获取病历记录
     */
    @GetMapping("/{id}")
    public Result<Medicalrecord> getMedicalRecordById(@PathVariable("id") Integer id) {
        Medicalrecord record = medicalrecordService.getById(id);
        if (record == null) {
            return Result.error(500, "未找到对应的病历记录");
        }
        return Result.success(record);
    }

    /**
     * 根据病历号获取病历记录
     */
    @GetMapping("/case/{caseNumber}")
    public Result<Medicalrecord> getMedicalRecordByCaseNumber(@PathVariable("caseNumber") String caseNumber) {
        Medicalrecord record = medicalrecordService.getByCaseNumber(caseNumber);
        if (record == null) {
            return Result.error(500, "未找到对应的病历记录");
        }
        return Result.success(record);
    }

    /**
     * 根据挂号ID获取病历记录
     */
    @GetMapping("/regist/{registId}")
    public Result<Medicalrecord> getMedicalRecordByRegistId(@PathVariable("registId") Integer registId) {
        Medicalrecord record = medicalrecordService.getByRegistId(registId);
        if (record == null) {
            return Result.error(500, "未找到对应的病历记录");
        }
        return Result.success(record);
    }

    /**
     * 创建新的病历记录
     */
    @PostMapping("/create")
    public Result<Medicalrecord> createMedicalRecord(@RequestBody Medicalrecord medicalrecord) {
        try {
            boolean success = medicalrecordService.save(medicalrecord);
            if (success) {
                return Result.success(medicalrecord);
            } else {
                return Result.error(500, "创建病历记录失败");
            }
        } catch (Exception e) {
            return Result.error(500, "创建病历记录时发生错误: " + e.getMessage());
        }
    }

    /**
     * 更新病历记录
     */
    @PutMapping("/update")
    public Result<Medicalrecord> updateMedicalRecord(@RequestBody Medicalrecord medicalrecord) {
        try {
            boolean success = medicalrecordService.updateById(medicalrecord);
            if (success) {
                return Result.success(medicalrecord);
            } else {
                return Result.error(500, "更新病历记录失败");
            }
        } catch (Exception e) {
            return Result.error(500, "更新病历记录时发生错误: " + e.getMessage());
        }
    }

    /**
     * 删除病历记录
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteMedicalRecord(@PathVariable("id") Integer id) {
        try {
            boolean success = medicalrecordService.removeById(id);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.error(500, "删除病历记录失败");
            }
        } catch (Exception e) {
            return Result.error(500, "删除病历记录时发生错误: " + e.getMessage());
        }
    }

    /**
     * 保存病历为模板
     */
    @PostMapping("/saveAsTemplate")
    public Result<String> saveAsTemplate(@RequestBody Medicalrecord medicalrecord) {
        try {
            boolean success = medicalrecordService.saveAsTemplate(medicalrecord);
            if (success) {
                return Result.success("保存为模板成功");
            } else {
                return Result.error(500, "保存为模板失败");
            }
        } catch (Exception e) {
            return Result.error(500, "保存为模板时发生错误: " + e.getMessage());
        }
    }

    /**
     * 根据病历状态查询病历记录
     */
    @GetMapping("/status/{status}")
    public Result<List<Medicalrecord>> getMedicalRecordsByStatus(@PathVariable("status") Integer status) {
        List<Medicalrecord> records = medicalrecordService.getByStatus(status);
        return Result.success(records);
    }

    /**
     * 导入模板到病历
     */
    @PostMapping("/importTemplate")
    public Result<Medicalrecord> importTemplate(@RequestParam("medicalRecordId") Integer medicalRecordId, 
                                              @RequestParam("templateId") Integer templateId) {
        try {
            Medicalrecord record = medicalrecordService.importTemplate(medicalRecordId, templateId);
            if (record != null) {
                return Result.success(record);
            } else {
                return Result.error(500, "导入模板失败");
            }
        } catch (Exception e) {
            return Result.error(500, "导入模板时发生错误: " + e.getMessage());
        }
    }

    /**
     * 导出病历为模板
     */
    @PostMapping("/exportTemplate")
    public Result<String> exportTemplate(@RequestParam("medicalRecordId") Integer medicalRecordId) {
        try {
            boolean success = medicalrecordService.exportTemplate(medicalRecordId);
            if (success) {
                return Result.success("导出模板成功");
            } else {
                return Result.error(500, "导出模板失败");
            }
        } catch (Exception e) {
            return Result.error(500, "导出模板时发生错误: " + e.getMessage());
        }
    }
}
